package com.xfanonymous.cloud.exp;


import com.xfanonymous.cloud.resp.ResultData;
import com.xfanonymous.cloud.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Copyright (c) 2023-2033 All Rights Reserved
 *
 * @Date: 2024/3/6
 * @Description: 全局异常处理
 *
 * 1. @Slf4j lombok日志工具，log.error()
 * 2. @RestControllerAdvice = @ResponseBody + @ControllerAdvice
 * 3. @ResponseStatus 用于异常类或Handler方法，返回异常状态码和描述信息
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Internal Server Error")
    public ResultData<String> exception(Exception e) {
        System.out.println("come in GlobalExceptionHandler");
        log.error("全局异常信息：{}", e.getMessage(), e);

        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
    }

    /**
     * 前端报错：
     * {
     *     "timestamp": "2024-03-06T09:31:00.990+00:00",
     *     "status": 500,
     *     "error": "Internal Server Error",
     *     "path": "/pay/get/-4"
     * }
     *
     * slf4j报错日志：
     * 2024-03-06T17:31:30.639+08:00 ERROR 23852 --- [nio-8001-exec-2] c.p.payment.exp.GlobalExceptionHandler   : 全局异常信息：id不能为负数
     *
     * consul报错：
     * java.lang.RuntimeException: id不能为负数
     */
}
